package com.entrogy.config;

import org.apache.catalina.connector.Connector;
import org.apache.coyote.http11.Http11AprProtocol;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author xlr
 * @date 2019-6
 */
@Configuration
public class TomcatConfig {

    @Value("${server.http.port}")
    private int httpPort;
    @Value("${server.https.port}")
    private int httpsPort;
    @Value("${ssl.all.path}")
    private String sslAllPath;
    @Value("${ssl.server-pem}")
    private String serverPem;
    @Value("${ssl.server-key}")
    private String serverKey;

    /*@Bean
    public EmbeddedServletContainerCustomizer containerCustomizer() {
        return new EmbeddedServletContainerCustomizer() {
            @Override
            public void customize(ConfigurableEmbeddedServletContainer container) {
                if (container instanceof TomcatEmbeddedServletContainerFactory) {
                    TomcatEmbeddedServletContainerFactory containerFactory =
                            (TomcatEmbeddedServletContainerFactory) container;

                    Connector connector = new Connector(TomcatEmbeddedServletContainerFactory.DEFAULT_PROTOCOL);
                    connector.setPort(httpPort);
                    containerFactory.addAdditionalTomcatConnectors(connector);
                }
            }
        };
    }*/
    @Bean
    public EmbeddedServletContainerCustomizer containerCustomizer() {
        return new EmbeddedServletContainerCustomizer() {
            @Override
            public void customize(ConfigurableEmbeddedServletContainer container) {
                if (container instanceof TomcatEmbeddedServletContainerFactory) {
                    TomcatEmbeddedServletContainerFactory containerFactory =
                            (TomcatEmbeddedServletContainerFactory) container;
                    Connector connector = new Connector("org.apache.coyote.http11.Http11AprProtocol");
                    connector.setScheme("http");
                    connector.setPort(httpPort);
                    containerFactory.addAdditionalTomcatConnectors(connector);
                    containerFactory.addAdditionalTomcatConnectors(createSslConnector());
                }
            }
        };
    }
    private Connector createSslConnector() {

        Connector connector = new Connector("org.apache.coyote.http11.Http11AprProtocol");
        Http11AprProtocol protocol = (Http11AprProtocol) connector.getProtocolHandler();

        connector.setPort(httpsPort);
        connector.setScheme("https");
        connector.setSecure(true);
        protocol.setSSLEnabled(true);
        protocol.setClientAuth("false");
        protocol.setSSLProtocol("TLSv1,TLSv1.1,TLSv1.2");
        protocol.setSSLCertificateKeyFile(sslAllPath+"/"+serverKey);
        protocol.setSSLCertificateFile(sslAllPath+"/"+serverPem);
        return connector;
    }
}
